// firstPackage/pages/reportModule/reportRecordDetail/reportRecordDetail.js

import {
  queryRecommendDetail,
  updateRecommendDetail
} from "../../../../service/custom_service"
import { queryReportHouseExpert } from "../../../../service/expert_service"
import {
  chooseMultipleImgUploadQiniuAction, chooseAndUploadQiniuActicon
} from "../../../../service/upload_service"
import { checkRecordAction, dockerDistributeAction } from "../../../../service/manager_service"
import { companyEmployeeList } from "../../../../service/team_service"
import debounce from "../../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    bottomTitle: "提交",

    stepActive: 0,//当前审核步骤

    detailId: null,//详情id
    formData: {

    },//详情数据
    //提交数据
    subFormData: {
      "id": 0,
      "isPass": true,
      "remark": "",
      "url": "",//确客二维码
      "isReportDeveloper": true,
      "width": 0,
      "height": 0,
      "size": 0
    },
    //报备列表
    recordList: [

    ],
    currentSubmitStatus: null,//最终提交数据的status
    isReportDeveloper: false,//是否报备公司
    dockerList: [],//对接人列表
    showDockerPop: false,//显示驻场选择弹窗
    selectDockerItem: {},//被选中的对接人
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        detailId: options.id
      })
    }
    this.queryRecommendDetail();
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  //查询报备详情:
  queryRecommendDetail() {
    wx.showLoading();
    queryRecommendDetail(this.data.detailId).then(res => {
      wx.hideLoading();
      console.log("JJLog:获取到的报备详情是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          formData: res.data,
          'subFormData.id': res.data.id,
          'subFormData.url': res.data.confirmCusQrCode || ""
        })
        this.companyEmployeeList();
        if (res.data.isReportDeveloper) {
          this.setData({
            isReportDeveloper: res.data.isReportDeveloper
          })
        }


      }
    }).catch(err => {
      console.error("JJLog:queryRecommendDetail", err);
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
    })
  },
  //根据房源查询报备对接人:
  companyEmployeeList() {
    companyEmployeeList(0, 999).then(res => {
      console.log("JJLog:获取到的项目驻场:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          dockerList: res.data || []
        })
        if (res.data && res.data.length > 0) {
          this.dealDockerList(res.data);
        }
      }
    }).catch(err => {

      console.error("JJLog:companyEmployeeList:", err);
    })
  },
  dealDockerList(arr) {
    arr.forEach((i) => {
      i.isChoose = false;
    })
    this.setData({
      dockerList: arr
    })
  },
  //分配驻场弹窗:
  distributionDockerAciton() {
    this.setData({
      showDockerPop: true
    })
  },


  //上传资料图片
  pushRecordImg() {
    if (this.data.isReportDeveloper) {
      wx.showToast({
        title: '已报备楼盘，不能再次上传确客码',
        icon: "none"
      })
      return;
    }
    chooseAndUploadQiniuActicon('record').then(res => {
      console.log("JJLog:图片上传成功:" + JSON.stringify(res));
      wx.showToast({
        title: '上传成功',
      })
      this.setData({
        'subFormData.url': res.complateUrl,
        'subFormData.height': res.height,
        "subFormData.width": res.width
      })
    }).catch(err => {
      console.log("JJLog:图片上传失败:" + JSON.stringify(err));
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })
  },
  //删除一张图片
  cancelOneImg(e) {
    let item = e.currentTarget.dataset.item;
    let imgIndex = e.currentTarget.dataset.imgindex;
    let index = e.currentTarget.dataset.index;
    let images = item.images;
    images.splice(imgIndex, 1);
    images.images = images;
    let changeItem = 'recordList[' + index + ']';
    this.setData({
      [changeItem]: recordItem,
      'subFormData.images': images,
    })
  },

  //提交数据
  submitInfoAction(e) {
    let isPass = e.currentTarget.dataset.ispass;
    this.setData({
      'subFormData.isPass': isPass
    })
    if (isPass) {
      //通过报备:
      wx.showModal({
        title: '提示',
        content: '确认已报备',
        confirmColor: "#FF0000",
        success: res => {
          if (res.confirm) {
            this.submitInfoRequest();
          }
        }
      })

    } else {
      //拒绝报备
      wx.showModal({
        title: '提示',
        content: '是否确定该报备无效',
        success: res => {
          if (res.confirm) {
            this.submitInfoRequest();
          }
        }
      })
    }
  },

  //提交数据:
  submitAction() {
    debounce(this.submitInfoRequest);
  },
  //提交数据请求;
  submitInfoRequest() {
    console.log(this.data.subFormData);
    wx.showLoading();
    checkRecordAction(this.data.subFormData).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000);
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:checkRecordAction", err);
    })
  },

  //选择对接人：
  selectDockAction(e) {
    let index = e.currentTarget.dataset.index;
    let arr = this.data.dockerList;
    let selectItem = {};
    arr.forEach((item, idx) => {
      item.isChoose = false;
      if (index == idx) {
        item.isChoose = true;
        selectItem = item;
      }
    })
    this.setData({
      dockerList: arr,
      selectDockerItem: selectItem
    })
  },
  //确定选择这个对接人
  sureSelectAccept() {
    if (JSON.stringify(this.data.selectDockerItem) == "{}") {
      wx.showToast({
        title: '请选择一个项目驻场',
        icon: "none"
      })
      return;
    }
    let dockerId = this.data.selectDockerItem.wxUserId;
    let reId = this.data.formData.id;
    this.dockerDistributeAction(dockerId, reId);

  },
  //重新分配驻场
  dockerDistributeAction(dId, rId) {
    wx.showLoading();
    dockerDistributeAction(dId, rId).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          showDockerPop: false,
          'formData.dockingName': this.data.selectDockerItem.realName,
          'formData.dockingPhone': this.data.selectDockerItem.phone,
        })
        wx.showToast({
          title: '分配成功',
          icon: "success"
        })
        this.queryRecommendDetail();
      }

    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:dockerDistributeAction", err);
    })
  },
  popupHide() {
    this.setData({
      showDockerPop: false
    })
  },



})